import validate from '../../utils/validate'
import { alterPassword } from '../../service/user'
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
import {logout} from '../../service/auth'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    old_password: '',
    password: '',
    password_confirmation: '',

    error_old_password: '',
    error_password: '',
    error_password_confirmation: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  checkInput() {
    const checkOldPassword = validate.min(this.data.old_password, 6)
    const checkPassword = validate.min(this.data.password, 6)
    if (!checkOldPassword) {
      this.setData({ error_old_password: '旧密码不能少于六位' })
    } else {
      this.setData({ error_old_password: '' })
    }
    if (checkPassword) {
      this.setData({ error_password: '' })
    } else {
      this.setData({ error_password: '密码不能少于六位' })
    }
    if (this.data.password == this.data.password_confirmation) {
      this.setData({ error_password_confirmation: '' })
    }else{
      this.setData({ error_password_confirmation: '两次输入密码不一致' })
    }
    if (this.data.old_password != '' && checkPassword && this.data.password == this.data.password_confirmation) return true
    return false

  },
  alterPassword() {
    console.log(11)
    if (!this.checkInput()) return
    const { old_password, password, password_confirmation } = this.data
    const data = {
      old_password,
      password: password,
      password_confirmation
    }
    alterPassword(data).then(() => {
    // 1.请求API,执行退出登录
    logout().then(() => {
      // 2.清楚缓存
      wx.removeStorageSync('access_token');
      wx.removeStorageSync('userInfo')
      Toast({
        type: 'success',
        message: '退出成功',
        onClose: () => {
          wx.reLaunch({
            url: '/pages/login/login',
          });
        },
      });
    })



    })
  }
})